package com.runa.rccprotocol.dto.huanxiang;

import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;
import com.runa.protocol.util.DigitalUtil;
import com.runa.protocol.util.UtilTime;
import com.runa.rccprotocol.dto.ConcentratorRequestData;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author diandian
 * @Date 2022/5/20 9:34
 */
public class HuanXiangEquRequestData extends ConcentratorRequestData {
    public HuanXiangEquRequestData(String address) {
        super(address);
    }

    private byte[] requestData;

    public void readHouseMeter(String meter) {
//        requestData = newComm byte[]{0x68, 0x20, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x03, 0x1F, (byte) 0x90, 0x01, 0, 0x16};
        requestData = new byte[]{(byte) 0xFE,(byte) 0xFE,(byte) 0xFE,0x68, 0x20, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x03,(byte) 0x90, (byte) 0x1F,  0x01, 0, 0x16};
//        requestData = newComm byte[]{0x68, 0x20, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x03, (byte) 0x1F,(byte) 0x90,  0x00, 0, 0x16};
        ByteUtil.tenstringToBcd(requestData, meter, 5, false);
        requestData[17] = ByteUtil.sum(requestData, 3, 14);
        setHaveResult(true);
    }

    public void readHouseMeter_Modbus(String meter) {
        int no = Integer.parseInt(meter);
        requestData = new byte[]{(byte)no,0x03,0x00,0x01,0x00,0x26, (byte) 0x95, (byte) 0xD0};
        requestData[0] = (byte) no;

        int value = DigitalUtil.calcCrc16(requestData, 0, 6);
        requestData[6] = Integer.valueOf(value & 0xff).byteValue();
        value = value >> 8;
        requestData[7] = Integer.valueOf(value & 0xff).byteValue();

        setHaveResult(true);
    }



    public void readValve(String valve){
        requestData = new byte[]{0x68, 0x46, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x09, 0x21, (byte) 0x90, 0x01,
//        requestData = newComm byte[]{0x68, 0x46, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x09, 0x1F, (byte) 0x90, 0x01,
                0, 0, 0, 0, 0, 0,
                0, 0x16};
        String time = UtilTime.getCurrentTime("yyMMddHHmmss");
        int length = time.length();
        int index = 13;
        requestData[++index] = (byte) Integer.parseInt(time.substring(length-2, length));
        requestData[++index] = (byte) Integer.parseInt(time.substring(length-4, length-2));
        requestData[++index] = (byte) Integer.parseInt(time.substring(length-6, length-4));
        requestData[++index] = (byte) Integer.parseInt(time.substring(length-8, length-6));
        requestData[++index] = (byte) Integer.parseInt(time.substring(length-10, length-8));
        requestData[++index] = (byte) Integer.parseInt(time.substring(length-12, length-10));
        ByteUtil.tenstringToBcd(requestData, valve, 2, false);
        requestData[++index] = ByteUtil.sum(requestData, 0, requestData.length-2);
        setHaveResult(true);
    }


    public void readHouseValve(String valve) {
        requestData = new byte[]{0x68, 0x46, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x09, 0x1F, (byte) 0x90, 0x01,
                0, 0, 0, 0, 0, 0,
                0, 0x16};
        String time = UtilTime.getCurrentTime("yyMMddHHmmss");
        int length = time.length();
        int index = 13;
        requestData[++index] = (byte) Integer.parseInt(time.substring(length-2, length));
        requestData[++index] = (byte) Integer.parseInt(time.substring(length-4, length-2));
        requestData[++index] = (byte) Integer.parseInt(time.substring(length-6, length-4));
        requestData[++index] = (byte) Integer.parseInt(time.substring(length-8, length-6));
        requestData[++index] = (byte) Integer.parseInt(time.substring(length-10, length-8));
        requestData[++index] = (byte) Integer.parseInt(time.substring(length-12, length-10));
        ByteUtil.tenstringToBcd(requestData, valve, 2, false);
        requestData[++index] = ByteUtil.sum(requestData, 0, requestData.length-2);
        setHaveResult(true);
    }


    public void readValveOpening(String valve) {
        requestData = new byte[]{0x68, 0x46, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x03, 0x48, (byte) 0xD1, 0x01,
                0, 0x16};
        ByteUtil.tenstringToBcd(requestData, valve, 2, false);
        requestData[14] = ByteUtil.sum(requestData, 0, 14);
        setHaveResult(true);
    }


    public void readHouseValveState(String valve) {
        requestData = new byte[]{0x68, 0x46, 0, 0, 0, 0, 0, 0, 0, 0x21, 0x03, 0x30, (byte) 0xD1, 0x01,
                0, 0x16};
        ByteUtil.tenstringToBcd(requestData, valve, 2, false);
        requestData[14] = ByteUtil.sum(requestData, 0, 14);
        setHaveResult(true);
    }

    public void buildValveCtr(String valve, Integer open) {
        requestData = new byte[]{0x68, 0x46, 0, 0, 0, 0, 0, 0, 0, 0x04, 0x06, 0x48,  (byte) 0xA0,0x01,
                0,0,0,
                0, 0x16};
        ByteUtil.tenstringToBcd(requestData, valve, 2, false);
        byte[] bytes = ByteUtil.shortToByte(Short.parseShort(open.toString()));
        requestData[15] = bytes[0];
        requestData[16] = bytes[1];
        requestData[17] = ByteUtil.sum(requestData, 0, 17);
        setHaveResult(true);
    }


    public void valveCtrl(String valve,boolean open, boolean lock) {
        requestData = new byte[]{0x68, 0x46, 0, 0, 0, 0, 0, 0, 0, 0x22, 0x04, 0x30, (byte) 0xA0, 0x01,
                0,
                0, 0x16};
        if (lock) {
            requestData[14] = (byte) (open ? 0x55 : 0x99);
        } else {
            requestData[14] = (byte) (open ? 0x66 : 0x77);
        }
        ByteUtil.tenstringToBcd(requestData, valve, 2, false);
        requestData[15] = ByteUtil.sum(requestData, 0, 15);
        setHaveResult(true);
    }

    @Override
    public byte[] getData() {
        return this.requestData;
    }

    @Override
    public boolean check(IResponseData data) {
        return true;
    }

//    public static void main(String[] args) {
////        String ss = "13180A1806160000";
////        byte[] bytes3 = ByteUtil.hexStringToByte(ss);
////        int length = bytes3.length;
////        for (int i = length-1; i >0 ; i--) {
////            System.out.print(bytes3[i]);
////
////        }
////        System.out.println();
////
////        System.out.println(ByteUtil.byteToLong(bytes3, 0, ByteOrder.HGFEDCBA));
////
////        HuanXiangEquRequestData requestData = newComm HuanXiangEquRequestData("");
////        requestData.readHouseValve("00000000001363");
////        System.out.println(ByteUtil.byteToHexString(requestData.getData()));
////
////        requestData.readHouseMeter_Modbus("01");
////        System.out.println(ByteUtil.byteToHexString(requestData.getData()));
////
////        String trrr = "01 03 00 01 00 26".replace(" ", "");
////        byte[] bytes = ByteUtil.hexStringToByte(trrr);
////
////        int value = DigitalUtil.calcCrc16(bytes, bytes.length);
////        byte[] ddd = newComm byte[2];
////
////        ByteUtil.shortToByte((short) value, ddd, 0, ByteOrder.AB);
////
////        System.out.println(ByteUtil.byteToHexString(ddd));
////
////
////        byte[] sss = ByteUtil.hexStringToByte("68 46 63 13 00 00 00 00 00 01 09 1F 90 01 34 20 09 07 06 16 5E 16".replace(" ", ""));
////        sss[sss.length-2] = ByteUtil.sum(sss, 0, sss.length-2);
////        System.out.println(ByteUtil.byteToHexString(sss));
////
////        byte[] bytes1 = newComm byte[2];
////        ByteUtil.intToBcd(bytes1, 0, 6410, true);
////        System.out.println(ByteUtil.byteToHexString(bytes1));
////
////
////        requestData.readValveOpening("00000000010893");
////
////        System.out.println(ByteUtil.byteToHexString(requestData.getData()));
////
////        byte[] bytes2 = ByteUtil.shortToByte(Short.parseShort("80".toString()));
////
////
////
////        System.out.println(ByteUtil.byteToShort(bytes2,0,ByteOrder.AB));
//
////        String s="01 03 00 01 00 26";
////        byte[] bytes = ByteUtil.hexStringToByte(s.replace(" ",""));
////        int i = DigitalUtil.calcCrc16(bytes, 0, 6);
////        System.out.println(i);
////        i = i >> 8;
////        System.out.println(Integer.valueOf(i & 0xff).byteValue());
//    }
}